package com.apache.uct.service.impl;

import java.util.List;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.uct.common.entity.Sys;
import com.apache.uct.manager.SysManager;
import com.apache.uct.util.UctStringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SysManagerImpl implements SysManager {

	protected IDao sysDao;
	protected final String entityName = "com.apache.uct.common.entity.Sys";
	private List<PluginConnector> plugins;
	private Logger log = LoggerFactory.getLogger(SysManager.class);

	public void setSysDao(IDao sysDao) {
		this.sysDao = sysDao;
	}

	public void setPlugins(List<PluginConnector> plugins) {
		this.plugins = plugins;
	}

	public String saveInfo(ParamsVo<Sys> vo) throws BusinessException {
		Sys sys = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="sys_"+infoId;
		sys.setSysId(infoId);
		MethodParam param = new MethodParam("Sys", cacheKey, "", entityName);
		param.setVaule(sys);
		if (sysDao.insert(param)) {
			if (!Validator.isEmpty(plugins)) {
				vo.setMethodKey("saveInfo");
				try {
					for (PluginConnector plugin : plugins) {
						plugin.execute(vo);
					}
				} catch (Exception e) {
					log.error("执行系统注册保存插件出错", e);
				}
			}
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<Sys> vo) throws BusinessException {
		Sys sys = vo.getObj();
		if (Validator.isNotNull(sys.getSysId())) {
			String cacheKey = "";
			//String cacheKey="sys_"+sys.getSysId();
			MethodParam param = new MethodParam("Sys", cacheKey, "", entityName);
			param.setVaule(sys);
			return sysDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<Sys> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="sys_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		Sys info = (Sys) sysDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		boolean flag = false;
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("Sys");
			flag = sysDao.edit(param);
		} else {
			param.setParams("sysId", infoId);
			param.setDelete(true);
			flag = sysDao.delete(param);
		}
		if (flag) {
			if (!Validator.isEmpty(plugins)) {
				vo.setObj(info);
				vo.setMethodKey("deleteInfo");
				try {
					for (PluginConnector plugin : plugins) {
						plugin.execute(vo);
					}
				} catch (Exception e) {
					log.error("执行系统注册保存插件出错", e);
				}
			}
		}
		return flag;
	}

	public Object getInfoById(ParamsVo<Sys> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="sys_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return sysDao.selectById(param);
	}

	public Object execute(ParamsVo<Sys> vo) {
		String key = vo.getKey();
		if ("checkSysEname".equals(key)) {
			Sys sys = vo.getObj();
			return checkRoleEname(sys.getSysEname());
		}
		return null;
	}

	private boolean checkRoleEname(String sysEname) {
		MethodParam param = new MethodParam("BySysEname", "", sysDao.getSql(1) + " and sysEname=:sysEname", entityName);
		param.setParams("sysEname", sysEname);
		long count = sysDao.count(param);
		if (count > 0)
			return false;
		return true;
	}

	public Page getPageInfo(ParamsVo<Sys> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "order_num";
		MethodParam param = setMethodParams(vo, 2, orderBy);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		param.setOrderby(orderBy);
		Page page = sysDao.pageSelect(param);
		return page;
	}

	public List<Sys> getList(ParamsVo<Sys> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "order_num";
		MethodParam param = setMethodParams(vo, 2, orderBy);
		param.setOrderby(orderBy);
		return sysDao.select(param);
	}

	public long countInfo(ParamsVo<Sys> vo) {
		MethodParam param = setMethodParams(vo, 1, "");
		return sysDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<Sys> vo, int type, String orderBy) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(sysDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		Sys sys = vo.getObj();
		if (!Validator.isEmpty(sys)) {
			if (Validator.isNotNull(sys.getSysEname())) {
				sb.append(" and sysEname like '%" + sys.getSysEname() + "%'");
				param.setParams("sysEname", "'%" + sys.getSysEname() + "%'");
			}
			if (Validator.isNotNull(sys.getSysCname())) {
				sb.append(" and sysCname like '%" + sys.getSysCname() + "%'");
				param.setParams("sysCname", "'%" + sys.getSysCname() + "%'");
			}
			if (Validator.isNotNull(sys.getSysPass())) {
				sb.append(" and sysPass =:sysPass");
				param.setParams("sysPass", sys.getSysPass());
			}
			if (Validator.isNotNull(sys.getCheckIp())) {
				sb.append(" and checkIp =:checkIp");
				param.setParams("checkIp", sys.getCheckIp());
			}
			if (Validator.isNotNull(sys.getAllowIplist())) {
				sb.append(" and allowIplist =:allowIplist");
				param.setParams("allowIplist", sys.getAllowIplist());
			}
			if (Validator.isNotNull(sys.getSysRemark())) {
				sb.append(" and sysRemark =:sysRemark");
				param.setParams("sysRemark", sys.getSysRemark());
			}
		}
		if (Validator.isNotNull(orderBy)) {
			sb.append(" order by " + UctStringUtil.toCamelNamed(orderBy));
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
